package com.gpframework.module.module.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.exception.BaseException;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.manufacture.domain.GpProductionDetails;
import com.gpframework.module.manufacture.domain.GpProductionScheduling;
import com.gpframework.module.manufacture.domain.GpWeightReductionRuleSetting;
import com.gpframework.module.manufacture.domain.vo.GpProductionDetailsVo;
import com.gpframework.module.manufacture.domain.vo.GpProductionSchedulingVo;
import com.gpframework.module.manufacture.service.IGpProductionDetailsService;
import com.gpframework.module.manufacture.service.IGpProductionSchedulingService;
import com.gpframework.module.manufacture.service.IGpWeightReductionRuleSettingService;
import com.gpframework.module.module.domain.*;
import com.gpframework.module.module.domain.req.GpIncomingInspectUpdate;
import com.gpframework.module.module.domain.vo.*;
import com.gpframework.module.module.mapper.GpIncomingInspectMapper;
import com.gpframework.module.module.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GpIncomingInspect
 * @Author gpframework
 * @Date 2023-12-11
 * @Version 1.0.0
 * @Description 来料检验单Service业务层处理
 */
@Service
public class GpIncomingInspectServiceImpl extends ServiceImpl<GpIncomingInspectMapper, GpIncomingInspect> implements IGpIncomingInspectService {


    @Autowired
    private IGpIncomingInspectWeightService iGpIncomingInspectWeightService;

    @Autowired
    private IGpIncomingInspectWidthService iGpIncomingInspectWidthService;

    @Autowired
    private IGpIncomingInspectYardVerifyService iGpIncomingInspectYardVerifyService;

    @Autowired
    private IGpGramWeightService iGpGramWeightService;

    @Autowired
    private IGpColorFastnessService iGpColorFastnessService;

    @Autowired
    private IGpFormaldehydeContrastService iGpFormaldehydeContrastService;

    @Autowired
    private IGpQualityfeedbackService iGpQualityfeedbackService;

    @Autowired
    private IGpQualityFlowDetailsService iGpQualityFlowDetailsService;

    @Autowired
    private IGpDeliveryNoteService iGpDeliveryNoteService;

    @Autowired
    private IGpProductionSchedulingService iGpProductionSchedulingService;

    @Autowired
    private IGpProductionDetailsService iGpProductionDetailsService;

    @Autowired
    private  IGpWeightReductionRuleSettingService iGpWeightReductionRuleSettingService;






    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-11
     * @Param [lqw]
     * @Return List<GpIncomingInspectVo>
     */
    @Override
    public List<GpIncomingInspectVo> listVo(QueryWrapper<GpIncomingInspect> lqw) {
        List<GpIncomingInspectVo> gpIncomingInspectVos = this.baseMapper.listVo(lqw);
        for (GpIncomingInspectVo gpIncomingInspectVo : gpIncomingInspectVos) {

            gpIncomingInspectVo.setYardVerifyList(iGpIncomingInspectYardVerifyService.listVo(new QueryWrapper<GpIncomingInspectYardVerify>().eq("incoming_inspect_id", gpIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));

            gpIncomingInspectVo.setWidthList(iGpIncomingInspectWidthService.listVo(new QueryWrapper<GpIncomingInspectWidth>().eq("incoming_inspect_id", gpIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));

            gpIncomingInspectVo.setWeightList(iGpIncomingInspectWeightService.listVo(new QueryWrapper<GpIncomingInspectWeight>().eq("incoming_inspect_id", gpIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));

            // 实际验码导出
            List<GpIncomingInspectYardVerifyVo> yardVerifyList = gpIncomingInspectVo.getYardVerifyList();
            if (!CollectionUtils.isEmpty(yardVerifyList)) {
                GpIncomingInspectYardVerifyVo gpIncomingInspectYardVerify;
                for (int i = 0; i < yardVerifyList.size(); i++) {
                    gpIncomingInspectYardVerify = yardVerifyList.get(i);
                    switch (i) {
                        case 0:
                            gpIncomingInspectVo.setYardOne(gpIncomingInspectYardVerify.getYard());
                            break;
                        case 1:
                            gpIncomingInspectVo.setYardTwo(gpIncomingInspectYardVerify.getYard());
                            break;
                        case 2:
                            gpIncomingInspectVo.setYardThree(gpIncomingInspectYardVerify.getYard());
                            break;
                        case 3:
                            gpIncomingInspectVo.setYardFour(gpIncomingInspectYardVerify.getYard());
                            break;
                        case 4:
                            gpIncomingInspectVo.setYardFive(gpIncomingInspectYardVerify.getYard());
                            break;
                        default:
                            break;
                    }
                }
            }
            // 幅宽导出
            List<GpIncomingInspectWidthVo> widthList = gpIncomingInspectVo.getWidthList();
            if (!CollectionUtils.isEmpty(widthList)) {
                GpIncomingInspectWidthVo gpIncomingInspectWidth;
                for (int i = 0; i < widthList.size(); i++) {
                    gpIncomingInspectWidth = widthList.get(i);
                    switch (i) {
                        case 0:
                            gpIncomingInspectVo.setStartWidthOne(gpIncomingInspectWidth.getStartWidth());
                            gpIncomingInspectVo.setMiddleWidthOne(gpIncomingInspectWidth.getMiddleWidth());
                            gpIncomingInspectVo.setEndWidthOne(gpIncomingInspectWidth.getEndWidth());
                            break;
                        case 1:
                            gpIncomingInspectVo.setStartWidthTwo(gpIncomingInspectWidth.getStartWidth());
                            gpIncomingInspectVo.setMiddleWidthTwo(gpIncomingInspectWidth.getMiddleWidth());
                            gpIncomingInspectVo.setEndWidthTwo(gpIncomingInspectWidth.getEndWidth());
                            break;
                        default:
                            break;
                    }
                }
            }
            // 重量导出
            List<GpIncomingInspectWeightVo> weightList = gpIncomingInspectVo.getWeightList();
            if (!CollectionUtils.isEmpty(weightList)) {
                GpIncomingInspectWeightVo gpIncomingInspectWeight;
                for (int i = 0; i < weightList.size(); i++) {
                    gpIncomingInspectWeight = weightList.get(i);
                    switch (i) {
                        case 0:
                            gpIncomingInspectVo.setWeightOne(gpIncomingInspectWeight.getWeight());
                            break;
                        case 1:
                            gpIncomingInspectVo.setWeightTwo(gpIncomingInspectWeight.getWeight());
                            break;
                        case 2:
                            gpIncomingInspectVo.setWeightThree(gpIncomingInspectWeight.getWeight());
                            break;
                        case 3:
                            gpIncomingInspectVo.setWeightFour(gpIncomingInspectWeight.getWeight());
                            break;
                        case 4:
                            gpIncomingInspectVo.setWeightFive(gpIncomingInspectWeight.getWeight());
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        return gpIncomingInspectVos;
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-11
     * @Param [id]
     * @Return GpIncomingInspectVo
     */
    @Override
    public GpIncomingInspectVo getVoById(Long id) {
        GpIncomingInspectVo gpIncomingInspectVo = this.baseMapper.getVo(id);
        gpIncomingInspectVo.setYardVerifyList(iGpIncomingInspectYardVerifyService.listVo(new QueryWrapper<GpIncomingInspectYardVerify>().eq("incoming_inspect_id", gpIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));
        gpIncomingInspectVo.setWidthList(iGpIncomingInspectWidthService.listVo(new QueryWrapper<GpIncomingInspectWidth>().eq("incoming_inspect_id", gpIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));
        gpIncomingInspectVo.setWeightList(iGpIncomingInspectWeightService.listVo(new QueryWrapper<GpIncomingInspectWeight>().eq("incoming_inspect_id", gpIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));

        // 实际验码导出
        List<GpIncomingInspectYardVerifyVo> yardVerifyList = gpIncomingInspectVo.getYardVerifyList();
        if (!CollectionUtils.isEmpty(yardVerifyList)) {
            GpIncomingInspectYardVerifyVo gpIncomingInspectYardVerify;
            for (int i = 0; i < yardVerifyList.size(); i++) {
                gpIncomingInspectYardVerify = yardVerifyList.get(i);
                switch (i) {
                    case 0:
                        gpIncomingInspectVo.setYardOne(gpIncomingInspectYardVerify.getYard());
                        break;
                    case 1:
                        gpIncomingInspectVo.setYardTwo(gpIncomingInspectYardVerify.getYard());
                        break;
                    case 2:
                        gpIncomingInspectVo.setYardThree(gpIncomingInspectYardVerify.getYard());
                        break;
                    case 3:
                        gpIncomingInspectVo.setYardFour(gpIncomingInspectYardVerify.getYard());
                        break;
                    case 4:
                        gpIncomingInspectVo.setYardFive(gpIncomingInspectYardVerify.getYard());
                        break;
                    default:
                        break;
                }
            }
        }
        // 幅宽导出
        List<GpIncomingInspectWidthVo> widthList = gpIncomingInspectVo.getWidthList();
        if (!CollectionUtils.isEmpty(widthList)) {
            GpIncomingInspectWidthVo gpIncomingInspectWidth;
            for (int i = 0; i < widthList.size(); i++) {
                gpIncomingInspectWidth = widthList.get(i);
                switch (i) {
                    case 0:
                        gpIncomingInspectVo.setStartWidthOne(gpIncomingInspectWidth.getStartWidth());
                        gpIncomingInspectVo.setMiddleWidthOne(gpIncomingInspectWidth.getMiddleWidth());
                        gpIncomingInspectVo.setEndWidthOne(gpIncomingInspectWidth.getEndWidth());
                        break;
                    case 1:
                        gpIncomingInspectVo.setStartWidthTwo(gpIncomingInspectWidth.getStartWidth());
                        gpIncomingInspectVo.setMiddleWidthTwo(gpIncomingInspectWidth.getMiddleWidth());
                        gpIncomingInspectVo.setEndWidthTwo(gpIncomingInspectWidth.getEndWidth());
                        break;
                    default:
                        break;
                }
            }
        }
        // 重量导出
        List<GpIncomingInspectWeightVo> weightList = gpIncomingInspectVo.getWeightList();
        if (!CollectionUtils.isEmpty(weightList)) {
            GpIncomingInspectWeightVo gpIncomingInspectWeight;
            for (int i = 0; i < weightList.size(); i++) {
                gpIncomingInspectWeight = weightList.get(i);
                switch (i) {
                    case 0:
                        gpIncomingInspectVo.setWeightOne(gpIncomingInspectWeight.getWeight());
                        break;
                    case 1:
                        gpIncomingInspectVo.setWeightTwo(gpIncomingInspectWeight.getWeight());
                        break;
                    case 2:
                        gpIncomingInspectVo.setWeightThree(gpIncomingInspectWeight.getWeight());
                        break;
                    case 3:
                        gpIncomingInspectVo.setWeightFour(gpIncomingInspectWeight.getWeight());
                        break;
                    case 4:
                        gpIncomingInspectVo.setWeightFive(gpIncomingInspectWeight.getWeight());
                        break;
                    default:
                        break;
                }
            }
        }
        return gpIncomingInspectVo;
    }

    @Override
    public boolean updateByWeightAndWidthAndYardVerify(List<GpIncomingInspectUpdate> gpIncomingInspectUpdateList) {
        for (GpIncomingInspectUpdate gpIncomingInspectUpdate : gpIncomingInspectUpdateList) {
            //判断订单号是否重复
            if (StringUtils.isNotBlank(gpIncomingInspectUpdate.getSn())) {
                List<GpIncomingInspect> gpIncomingInspectList = this.list(new QueryWrapper<GpIncomingInspect>());
                for (GpIncomingInspect gpIncomingInspect : gpIncomingInspectList) {
                    if (gpIncomingInspectUpdate.getSn().equals(gpIncomingInspect.getSn())){
                        throw new BaseException("订单号重复");
                    } 
                }
            }
            this.editIncomingInspect(gpIncomingInspectUpdate);
            //更新上一级数据（修改到货单、做货明细、排单表）
            this.updateGpIncomingInspect(gpIncomingInspectUpdate);
        }
        return true;
    }

    private void updateGpIncomingInspect(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        GpDeliveryNote gpDeliveryNote = iGpDeliveryNoteService.getById(gpIncomingInspectUpdate.getDeliveryNoteId());
        GpProductionScheduling gpProductionScheduling = iGpProductionSchedulingService.getById(gpIncomingInspectUpdate.getProductionSchedulingId());
        GpProductionDetails gpProductionDetails = iGpProductionDetailsService.getById(gpIncomingInspectUpdate.getProductionDetailsId());
        //修改到货单数据
        gpDeliveryNote.setSupplierName(gpIncomingInspectUpdate.getSupplierName());
        gpDeliveryNote.setDyeingFactoryName(gpIncomingInspectUpdate.getSupplierName());
        gpDeliveryNote.setBatchNo(gpIncomingInspectUpdate.getBatchNo());
        gpDeliveryNote.setDyeVatNo(gpIncomingInspectUpdate.getDyeVatNo());
        gpDeliveryNote.setProductName(gpIncomingInspectUpdate.getProductName());
        gpDeliveryNote.setColour(gpIncomingInspectUpdate.getColour());
        gpDeliveryNote.setQuantity(gpIncomingInspectUpdate.getQuantity());
        gpDeliveryNote.setTotalQuantity(gpIncomingInspectUpdate.getTotalQuantity());

        iGpDeliveryNoteService.updateById(gpDeliveryNote);

        //修改排单表
        gpProductionScheduling.setSupplierName(gpIncomingInspectUpdate.getSupplierName());
        gpProductionScheduling.setProductName(gpIncomingInspectUpdate.getProductName());
        gpProductionScheduling.setDyeingFactoryName(gpIncomingInspectUpdate.getSupplierName());
        gpProductionScheduling.setDyeVatNo(gpIncomingInspectUpdate.getDyeVatNo());
        gpProductionScheduling.setProductName(gpIncomingInspectUpdate.getProductName());
        gpProductionScheduling.setColour(gpIncomingInspectUpdate.getColour());
        gpProductionScheduling.setQuantity(gpIncomingInspectUpdate.getQuantity());

        iGpProductionSchedulingService.updateById(gpProductionScheduling);

        //修改做货单
        gpProductionDetails.setCodingSpecifications(gpIncomingInspectUpdate.getCodingSpecifications());
        gpProductionDetails.setTagSpecifications(gpProductionDetails.getTagSpecifications());

        iGpProductionDetailsService.updateById(gpProductionDetails);




    }

    private boolean editIncomingInspect(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        List<GpIncomingInspectWeight> gpIncomingInspectWeightList = gpIncomingInspectUpdate.getGpIncomingInspectWeightList();
        for (GpIncomingInspectWeight gpIncomingInspectWeight : gpIncomingInspectWeightList) {
            gpIncomingInspectWeight.setIncomingInspectId(gpIncomingInspectUpdate.getId());
        }
        iGpIncomingInspectWeightService.saveOrUpdateBatch(gpIncomingInspectWeightList);

        List<GpIncomingInspectWidth> gpIncomingInspectWidthList = gpIncomingInspectUpdate.getGpIncomingInspectWidthList();
        for (GpIncomingInspectWidth gpIncomingInspectWidth : gpIncomingInspectWidthList) {
            gpIncomingInspectWidth.setIncomingInspectId(gpIncomingInspectUpdate.getId());
        }
        iGpIncomingInspectWidthService.saveOrUpdateBatch(gpIncomingInspectWidthList);

        List<GpIncomingInspectYardVerify> gpIncomingInspectYardVerifyList = gpIncomingInspectUpdate.getGpIncomingInspectYardVerifyList();
        for (GpIncomingInspectYardVerify gpIncomingInspectYardVerify : gpIncomingInspectYardVerifyList) {
            gpIncomingInspectYardVerify.setIncomingInspectId(gpIncomingInspectUpdate.getId());
        }
        iGpIncomingInspectYardVerifyService.saveOrUpdateBatch(gpIncomingInspectYardVerifyList);

        GpIncomingInspect gpIncomingInspect = Convert.convert(new TypeReference<GpIncomingInspect>() {}, gpIncomingInspectUpdate);
        return this.updateById(gpIncomingInspect);
    }

    @Override
    public boolean edit(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        return this.editIncomingInspect(gpIncomingInspectUpdate);
    }


    /**
     * 生成 克重表
     * @param gpIncomingInspectUpdate
     * @return
     */
    @Override
    public boolean addWeightMeter(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        List<Long> ids = gpIncomingInspectUpdate.getIds();

        GpIncomingInspectVo gpIncomingInspectVo;
        GpProductionDetailsVo gpProductionDetailsVo;
        GpGramWeight gpGramWeight;

        List<GpGramWeight> gpGramWeights=new ArrayList<>();

        Date now = new Date();
        for (Long id : ids) {
            gpIncomingInspectVo = this.getVoById(id);
            if (gpIncomingInspectVo == null) {
                continue;
            }
            gpGramWeight = new GpGramWeight();
            BeanUtils.copyProperties(gpIncomingInspectVo,gpGramWeight);
            if (StringUtils.isNotBlank(gpIncomingInspectUpdate.getCreateBy())) {
                gpGramWeight.setCreateBy(gpIncomingInspectUpdate.getCreateBy());
            }
            // 获取做货要求明细表中的标准克重
            gpProductionDetailsVo= iGpProductionDetailsService.getVoById(gpIncomingInspectVo.getProductionDetailsId());
            if (gpProductionDetailsVo!=null && StringUtils.isNotBlank(gpProductionDetailsVo.getGramWeight())){
                try {
                    gpGramWeight.setStandardWeight(new BigDecimal(gpProductionDetailsVo.getGramWeight()));
                } catch (NumberFormatException e) {
                }
            }
            gpGramWeight.setIncomingInspectId(gpIncomingInspectVo.getId());
            gpGramWeight.setCheckTime(now);

            GpWeightReductionRuleSetting one = iGpWeightReductionRuleSettingService.getOne(new LambdaQueryWrapper<GpWeightReductionRuleSetting>().eq(GpWeightReductionRuleSetting::getProductName, gpProductionDetailsVo.getProductName()).eq(GpWeightReductionRuleSetting::getDelStatus, YesNoEnum.NO.getKey()));
            if (one!=null){
                gpGramWeight.setStandardWeight(new BigDecimal(one.getStandardWeight()));
            }

            gpGramWeights.add(gpGramWeight);
        }
        return iGpGramWeightService.saveBatch(gpGramWeights);
    }

    /**
     * 生成色牢表
     * @param gpIncomingInspectUpdate
     * @return
     */
    @Override
    public boolean addColorFastness(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        List<Long> ids = gpIncomingInspectUpdate.getIds();

        GpIncomingInspectVo gpIncomingInspectVo;

        List<GpColorFastness> gpColorFastnessList=new ArrayList<>();
        for (Long id : ids) {
            gpIncomingInspectVo = this.getVoById(id);
            GpColorFastness gpColorFastness = new GpColorFastness();
            if (gpIncomingInspectVo != null) {
                BeanUtils.copyProperties(gpIncomingInspectVo,gpColorFastness);
            }
            if (StringUtils.isNotBlank(gpIncomingInspectUpdate.getCreateBy())) {
                gpColorFastness.setCreateBy(gpIncomingInspectUpdate.getCreateBy());
            }

            gpColorFastness.setCheckTime(new Date());
            gpColorFastness.setIncomingInspectId(gpIncomingInspectVo.getId());
            gpColorFastnessList.add(gpColorFastness);
        }

        return iGpColorFastnessService.saveBatch(gpColorFastnessList);
    }

    @Override
    public boolean addFormalde(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        List<Long> ids = gpIncomingInspectUpdate.getIds();
        GpIncomingInspectVo gpIncomingInspectVo;

        List<GpFormaldehydeContrast> gpFormaldehydeContrastList=new ArrayList<>();
        for (Long id : ids) {
            gpIncomingInspectVo = this.getVoById(id);
            GpFormaldehydeContrast gpFormaldehydeContrast = new GpFormaldehydeContrast();
            if (gpIncomingInspectVo != null) {
                BeanUtils.copyProperties(gpIncomingInspectVo,gpFormaldehydeContrast);
            }
            if (StringUtils.isNotBlank(gpIncomingInspectUpdate.getCreateBy())) {
                gpFormaldehydeContrast.setCreateBy(gpIncomingInspectUpdate.getCreateBy());
            }

            gpFormaldehydeContrast.setCheckTime(new Date());
            gpFormaldehydeContrast.setIncomingInspectId(gpIncomingInspectVo.getId());
            gpFormaldehydeContrastList.add(gpFormaldehydeContrast);
        }
        return iGpFormaldehydeContrastService.saveBatch(gpFormaldehydeContrastList);
    }

    @Override
    public boolean addQualityfeedback(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        List<Long> ids = gpIncomingInspectUpdate.getIds();
        GpIncomingInspectVo gpIncomingInspectVo;

        List<GpQualityfeedback> gpQualityfeedbackList=new ArrayList<>();
        Date now = new Date();
        for (Long id : ids) {
            gpIncomingInspectVo = this.getVoById(id);
            GpQualityfeedback gpQualityfeedback = new GpQualityfeedback();
            if (gpIncomingInspectVo != null) {
                BeanUtils.copyProperties(gpIncomingInspectVo,gpQualityfeedback);
            }
            if (StringUtils.isNotBlank(gpIncomingInspectUpdate.getCreateBy())) {
                gpQualityfeedback.setCreateBy(gpIncomingInspectUpdate.getCreateBy());
            }
            gpQualityfeedback.setIncomingInspectId(gpIncomingInspectVo.getId());
            gpQualityfeedback.setFeedbackId(gpIncomingInspectUpdate.getFeedbackId());
            gpQualityfeedback.setFeedbackName(gpIncomingInspectUpdate.getFeedbackName());
            gpQualityfeedback.setFeedbackType(gpIncomingInspectUpdate.getFeedbackType());
            gpQualityfeedback.setFeedbackContent(gpIncomingInspectUpdate.getFeedbackContent());
            gpQualityfeedback.setFeedbackTime(now);
            gpQualityfeedbackList.add(gpQualityfeedback);

        }
        return iGpQualityfeedbackService.saveBatch(gpQualityfeedbackList);
    }

    @Override
    public boolean qualityIssueTrackDetails(GpIncomingInspectUpdate gpIncomingInspectUpdate) {
        List<Long> ids = gpIncomingInspectUpdate.getIds();
        GpQualityfeedbackVo gpQualityfeedbackVo;
        GpDeliveryNoteVo gpDeliveryNoteVo;
        GpIncomingInspectVo gpIncomingInspectVo;
        GpProductionSchedulingVo gpProductionSchedulingVo;

        List<GpQualityFlowDetails> gpQualityFlowDetailsList=new ArrayList<>();
        for (Long id : ids) {
            gpQualityfeedbackVo = iGpQualityfeedbackService.getVoById(id);
            GpQualityFlowDetails gpQualityFlowDetails = new GpQualityFlowDetails();
            if (gpQualityfeedbackVo == null) {
                continue;
            }
            BeanUtils.copyProperties(gpQualityfeedbackVo,gpQualityFlowDetails);
            if (StringUtils.isNotBlank(gpIncomingInspectUpdate.getCreateBy())) {
                gpQualityFlowDetails.setCreateBy(gpIncomingInspectUpdate.getCreateBy());
            }
            gpIncomingInspectVo = this.getVoById(gpQualityfeedbackVo.getIncomingInspectId());
            if (gpIncomingInspectVo != null) {
                gpQualityFlowDetails.setDyeVatNo(gpIncomingInspectVo.getDyeVatNo());
                gpQualityFlowDetails.setQuantity(gpIncomingInspectVo.getQuantity());
                gpQualityFlowDetails.setEmptyQuantity(gpIncomingInspectVo.getTotalQuantity());
                gpDeliveryNoteVo =  iGpDeliveryNoteService.getVoById(gpIncomingInspectVo.getDeliveryNoteId());
                if (gpDeliveryNoteVo != null) {
                    gpQualityFlowDetails.setSendOutGoodsTime(gpDeliveryNoteVo.getSendOutGoodsTime());
                }
                gpProductionSchedulingVo= iGpProductionSchedulingService.getVoById(gpIncomingInspectVo.getProductionSchedulingId());
                if (gpProductionSchedulingVo != null) {
                    gpQualityFlowDetails.setSupplierName(gpProductionSchedulingVo.getSupplierName());
                }
            }
            gpQualityFlowDetails.setQualityFeedbackId(gpQualityfeedbackVo.getId());
            gpQualityFlowDetails.setProblemDescription(gpQualityfeedbackVo.getFeedbackContent());
            gpQualityFlowDetails.setProcessingScheme(gpQualityfeedbackVo.getReplyContent());
            gpQualityFlowDetailsList.add(gpQualityFlowDetails);

        }
        return iGpQualityFlowDetailsService.saveBatch(gpQualityFlowDetailsList);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpIncomingInspect gpIncomingInspect = this.getById(id);
        if (gpIncomingInspect == null) {
            return false;
        }
        List<GpIncomingInspectYardVerify> yardVerifyList = iGpIncomingInspectYardVerifyService.list(new LambdaQueryWrapper<GpIncomingInspectYardVerify>().eq(GpIncomingInspectYardVerify::getDelStatus, YesNoEnum.NO.getKey()).eq(GpIncomingInspectYardVerify::getIncomingInspectId, gpIncomingInspect.getId()));
        List<GpIncomingInspectWeight> weightList = iGpIncomingInspectWeightService.list(new LambdaQueryWrapper<GpIncomingInspectWeight>().eq(GpIncomingInspectWeight::getDelStatus, YesNoEnum.NO.getKey()).eq(GpIncomingInspectWeight::getIncomingInspectId, gpIncomingInspect.getId()));
        List<GpIncomingInspectWidth> widthList = iGpIncomingInspectWidthService.list(new LambdaQueryWrapper<GpIncomingInspectWidth>().eq(GpIncomingInspectWidth::getDelStatus, YesNoEnum.NO.getKey()).eq(GpIncomingInspectWidth::getIncomingInspectId, gpIncomingInspect.getId()));
        for (int i = 0; i < copyNum; i++) {
            gpIncomingInspect.setId(null);
            gpIncomingInspect.setSn(this.generateOrderNumber());
            this.save(gpIncomingInspect);
            // 复制加工来料检验单实际验码子表
            yardVerifyList.forEach(yardVerify -> {
                yardVerify.setId(null);
                yardVerify.setIncomingInspectId(gpIncomingInspect.getId());
            });
            iGpIncomingInspectYardVerifyService.saveBatch(yardVerifyList);
            // 复制加工来料检验单甲醛PM值子表
            weightList.forEach(formaldehyde -> {
                formaldehyde.setId(null);
                formaldehyde.setIncomingInspectId(gpIncomingInspect.getId());
            });
            iGpIncomingInspectWeightService.saveBatch(weightList);
            // 复制加工来料检验单幅宽子表
            widthList.forEach(width -> {
                width.setId(null);
                width.setIncomingInspectId(gpIncomingInspect.getId());
            });
            iGpIncomingInspectWidthService.saveBatch(widthList);
        }
        return true;
    }

    //生成订单号 格式  yy-MM-dd-序号 序号第二天归零
    public  String generateOrderNumber() {

        LocalDate lastDate = null;  // 上次生成订单号的日期
        int orderCount = 0;  // 订单序号
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 判断是否为新的一天，如果是则将序号归零
        if (lastDate == null || !currentDate.isEqual(lastDate)) {
            orderCount = 0;
            lastDate = currentDate;
        }

        // 格式化日期为字符串，年份只保留后两位
        String formattedDate = currentDate.format(DateTimeFormatter.ofPattern("yy-MM-dd"));

        // 生成序号（这里简化处理，每次调用序号加1）
        orderCount++;

        // 拼接订单号
        String orderNumber = formattedDate + "-" + orderCount;

        return orderNumber;
    }
}
